#include "weather.h"
#include "ui_weather.h"
#include <QString>
#include "myview.h"

weather::weather(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::weather)
{
    ui->setupUi(this);
    //隐藏标题栏
    this->setWindowFlag(Qt::FramelessWindowHint);
    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
              this, SLOT(replyFinished(QNetworkReply*)));

    manager->get(QNetworkRequest(QUrl("http://t.weather.sojson.com/api/weather/city/101250101")));

    pModel = new QStandardItemModel(6,15,ui->tableView);
    //设置数据模型的表头
    QStringList list = QStringList()<<"";
    pModel->setHorizontalHeaderLabels(list);//设置数据的表头

    ui->tableView->setModel(pModel);
    //ui->tableView->setRowHeight(0,75);
    for (int i=0;i<16;i++) {
        ui->tableView->setColumnWidth(i,90);
//        ui->tableView->setColumnHidden(100,i);
    }
    ui->tableView->setRowHeight(2,70);
    ui->tableView->setRowHeight(4,50);
    ui->tableView->setRowHeight(3,50);
    this->setWindowTitle("15天天气预报");
    for (int i=0;i<16;i++) {
        QModelIndex index=pModel->index(3,i);
        if(pModel->data(index)=="晴"){

        }
    }
    Myview * view=new Myview(ui->tableView);
    ui->tableView->setItemDelegateForRow(2,view);
}

weather::~weather()
{
    delete ui;
}

void weather::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),QPixmap("d:/image/2.png"));
}
void weather::replyFinished(QNetworkReply *reply)
{
    qDebug()<<"-----"<<endl;
    qDebug()<<reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)<<endl;
    if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200)
        {
            qDebug()<<"天气预报"<<"访问服务器成功"<<endl;
//            qDebug()<<QString(reply->readAll())<<endl;

        pareJson(reply->readAll());
        }
}

void weather::pareJson(QByteArray byteArray)
{
    QJsonDocument doc=QJsonDocument::fromJson(byteArray);
    if(doc.isObject()==true){
        qDebug()<<"该文档是一个对象"<<endl;
        QJsonObject obj1=doc.object();
        if(obj1.contains("cityInfo")==true){           
            QJsonValue var1=obj1.value("cityInfo");
            QJsonObject cityobj =var1.toObject();
            if(cityobj.contains("city")==true){
                var1=cityobj.value("city");
                qDebug()<<var1.toString()<<endl;
                ui->labelcity->setText(var1.toString());
            }
        }
        if(obj1.contains("data")==true){
            qDebug()<<"对象有天气数据"<<endl;
            QJsonValue var=obj1.value("data");
            QJsonObject dataObl =var.toObject();
            if(dataObl.contains("yesterday")==false){
                qDebug()<<"数据没有昨天的天气"<<endl;
                return;
            }
            //dataObj有数据，拿出数据
            var=dataObl.value("wendu");//当天温度           
            QString s = "°C";
            QString s1=var.toString();
            s1.append(s);
            ui->label_wendu->setText(s1);
            var=dataObl.value("quality");//空气质量
            ui->label_quality->setText(var.toString());
            var=dataObl.value("pm25");//pm2.5
            int a=var.toInt();
            QString str = QString::number(a);
            ui->label_25->setText(str);
            var=dataObl.value("pm10");//pm10
             a=var.toInt();
             str = QString::number(a);
            ui->label_10->setText(str);
            var=dataObl.value("shidu");//湿度
            ui->label_shidu->setText(var.toString());
            var=dataObl.value("ganmao");//提示
            ui->label_notice->setText(var.toString());
            var=dataObl.value("yesterday");//yesterday作为对象
            QJsonObject yesterObj =var.toObject();
            if(yesterObj.contains("ymd")){
                QJsonValue y=yesterObj.value("ymd");
                ymd=y.toString();
            }
            if(yesterObj.contains("week")){
                QJsonValue w=yesterObj.value("week");
                week=w.toString();
            }
            if(yesterObj.contains("type")){
                QJsonValue w=yesterObj.value("type");
                type=w.toString();
            }
            if(yesterObj.contains("high")){
                QJsonValue w=yesterObj.value("high");
                high=w.toString();
            }
            if(yesterObj.contains("low")){
                QJsonValue w=yesterObj.value("low");
                low=w.toString();
            }
            if(yesterObj.contains("fx")){
                QJsonValue w=yesterObj.value("fx");
                fx=w.toString();
            }
            if(yesterObj.contains("fl")){
                QJsonValue w=yesterObj.value("fl");
                fl=w.toString();
            }
            QStandardItem *item = new QStandardItem(week);//周
            pModel->setItem(0, 0, item);
            item = new QStandardItem(ymd);
            pModel->setItem(1, 0, item);
            item = new QStandardItem(type);
            pModel->setItem(2, 0, item);
            item = new QStandardItem(high);
            pModel->setItem(3, 0, item);
            item = new QStandardItem(low);
            pModel->setItem(4, 0, item);
            item = new QStandardItem(fx);
            pModel->setItem(5, 0, item);
            item = new QStandardItem(fl);
            pModel->setItem(6, 0, item);
            //接下来15天的天气
            if(dataObl.contains("forecast")==false){
                qDebug()<<"数据没有15天的天气"<<endl;
                return;
            }
            var=dataObl.value("forecast");
            qDebug()<<"对象中有天气预报"<<endl;
            if(var.isArray()){
                QJsonArray arry=var.toArray();
                qDebug()<<"搜索到"<<arry.size()<<"天的天气"<<endl;
                for (int i=0;i<15;i++) {
                    QJsonValue day;
                    if(arry[i].isObject()){
                        QJsonObject obj2=arry[i].toObject();
                        if(obj2.contains("ymd")){
                            day=obj2.value("ymd");
                            ymd=day.toString();
                        }
                        if(obj2.contains("week")){
                            day=obj2.value("week");
                            week=day.toString();
                        }
                        if(obj2.contains("type")){
                            day=obj2.value("type");
                            type=day.toString();
                        }
                        if(obj2.contains("high")){
                            day=obj2.value("high");
                            high=day.toString();
                        }
                        if(obj2.contains("low")){
                            day=obj2.value("low");
                            low=day.toString();
                        }
                        if(obj2.contains("fx")){
                            day=obj2.value("fx");
                            fx=day.toString();
                        }
                        if(obj2.contains("fl")){
                            day=obj2.value("fl");
                            fl=day.toString();
                        }

                    }
//                    qDebug()<<"数据获取完成"<<endl;
                    QStandardItem *item = new QStandardItem(week);//周
                    pModel->setItem(0, i+1, item);
                    item = new QStandardItem(ymd);
                    pModel->setItem(1, i+1, item);
                    item = new QStandardItem(type);
                    pModel->setItem(2, i+1, item);
                    item = new QStandardItem(high);
                    pModel->setItem(3, i+1, item);
                    item = new QStandardItem(low);
                    pModel->setItem(4, i+1, item);
                    item = new QStandardItem(fx);
                    pModel->setItem(5, i+1, item);
                    item = new QStandardItem(fl);
                    pModel->setItem(6, i+1, item);
                    if(i==1){
                        ui->label_time->setText(ymd);
                        ui->labelweek->setText(week);
                        ui->label_2->setText(type);
                    }
                }//end of for(;;)
            }//end of if(var.isArray())

        }//end of if(obj1.contains("data")==true)

    }
}

void weather::on_pushButton_clicked()
{
    close();
}
